package com.atguigu.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.apache.commons.dbutils.DbUtils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

	private static DruidDataSource dataSource;
    private static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();

	static {
		Properties properties = new Properties();
		InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
		try {
			properties.load(is);
			// 创建数据库连接池
			dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}




    // 获取数据库连接池的连接
    public static Connection getConnection(){
		Connection conn = conns.get();
		if(conn == null){
            try {
                conn = dataSource.getConnection();   // 从数据库中获得数据库连接
                conns.set(conn);   // 保存到ThreadLocal对象中，供后面的jdbc操作使用
                conn.setAutoCommit(false);   // 设置手动管理事务
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		return conn;
    }

    /**
     * 回滚事务，关闭连接
     */
	public static void rollbackAndClose(){
        Connection conn = conns.get();
        if(conn != null){
            try {
                conn.rollback();  // 回滚事务
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    conn.close();  // 关闭连接
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        conns.remove();   // 一定要remove操作，因为Tomcat底层使用了线程池
    }

    /**
     * 提交事务，关闭连接
     */
    public static void commitAndClose(){
        Connection conn = conns.get();
        if(conn != null){
            try {
                conn.commit();  // 提交事务
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    conn.close();  // 关闭连接
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        conns.remove();   // 一定要remove操作，因为Tomcat底层使用了线程池
    }


    public static void CloseResource(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        conns.remove();
    }

    public static void CloseResource(PreparedStatement ps, Connection conn, ResultSet rs) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        //等同于上面的
//        DbUtils.closeQuietly(conn, ps, rs);
    }
}

